const arr = [1, 2, 3, 4, [1, 2, 3, [1, 2, 3, [1, 2, 3]]], 5, "string", { name: "嗨害嗨同学" }];
// 1. concat + 递归
function flat1(arr) {
    let res = [];
    arr.forEach(item => {
        if (Array.isArray(item)) {
            res.concat(flat(item));
        } else {
            res.push(item);
        }
    })
    return res;
}
// console.log(flat1(arr));

function flat2(arr) {
    const res = arr.reduce((prev, cur) => {
        return prev.concat(Array.isArray(cur) ? flat2(cur) : cur);
    }, []);
    return res;
}
// console.log(flat2(arr));

function flat(arr) {
    const res = arr.reduce((prev, cur) => {
        return prev.concat(Array.isArray(cur) ? flat(cur) : cur);
    }, []);
    return res;
}
console.log(JSON.stringify(flat(arr)));

function flatByLevel(arr, num) {
    return num > 0 
    ? arr.reduce((prev, cur) => {
        return prev.concat(Array.isArray(cur) ? flatByLevel(cur, num - 1) : cur)
    }, [])
    : arr.slice();
}
// console.log(JSON.stringify(flatByLevel(arr, 1)));


const delSameElement = [1, 1, '2', 2, 3, 3, 3, 6];
const res = [...new Set(delSameElement)];
console.log(res);


// url解析
var url = "https://www.baidu.com/m?f=8&ie=utf-8&rsv_bp=1&tn=monline_3_dg&wd=session";
function solution(url) {
    let str = url.split('?')[1];
    const query = str.split('&');
    let res = {};
    for (let str of query) {
        let [key, value] = str.split('=');
        res[key] = value;
    }
    return res;
}
console.log(solution(url));
